package com.ruoyi.mall.controller;

import java.util.List;

import com.ruoyi.mall.entity.MallProperty;
import com.ruoyi.mall.service.IMallPropertyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.entity.MallValue;
import com.ruoyi.mall.service.IMallValueService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品属性值Controller
 * 
 * @author ruoyi
 * @date 2020-05-20
 */
@Controller
@RequestMapping("/mall/value")
public class MallValueController extends BaseController
{
    private String prefix = "mall/value";

    @Autowired
    private IMallValueService mallValueService;

    @Autowired
    private IMallPropertyService mallPropertyService;

    @RequiresPermissions("mall:value:view")
    @GetMapping()
    public String value(Long propertyId, ModelMap mmap)
    {

        mmap.addAttribute("propertyId",propertyId);
        mmap.addAttribute("mallPropertys",getMallPropertys());
        return prefix + "/value";
    }

    /**
     * 查询商品属性值列表
     */
    @RequiresPermissions("mall:value:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MallValue mallValue)
    {
        startPage();
        List<MallValue> list = mallValueService.selectMallValueList(mallValue);
        return getDataTable(list);
    }
    public List<MallProperty> getMallPropertys(){
        return mallPropertyService.selectMallPropertyList(null);
    }
    /**
     * 导出商品属性值列表
     */
    @RequiresPermissions("mall:value:export")
    @Log(title = "商品属性值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MallValue mallValue)
    {
        List<MallValue> list = mallValueService.selectMallValueList(mallValue);
        ExcelUtil<MallValue> util = new ExcelUtil<MallValue>(MallValue.class);
        return util.exportExcel(list, "value");
    }

    /**
     * 新增商品属性值
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.addAttribute("mallPropertys",getMallPropertys());
        return prefix + "/add";
    }

    /**
     * 新增保存商品属性值
     */
    @RequiresPermissions("mall:value:add")
    @Log(title = "商品属性值", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MallValue mallValue)
    {
        return toAjax(mallValueService.insertMallValue(mallValue));
    }

    /**
     * 修改商品属性值
     */
    @GetMapping("/edit/{valueId}")
    public String edit(@PathVariable("valueId") Long valueId, ModelMap mmap)
    {
        MallValue mallValue = mallValueService.selectMallValueById(valueId);
        mmap.put("mallValue", mallValue);
        mmap.addAttribute("mallPropertys",getMallPropertys());
        return prefix + "/edit";
    }

    /**
     * 修改保存商品属性值
     */
    @RequiresPermissions("mall:value:edit")
    @Log(title = "商品属性值", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MallValue mallValue)
    {
        return toAjax(mallValueService.updateMallValue(mallValue));
    }

    /**
     * 删除商品属性值
     */
    @RequiresPermissions("mall:value:remove")
    @Log(title = "商品属性值", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mallValueService.deleteMallValueByIds(ids));
    }
}
